package com.chixing.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.mapper.TemplateKillMapper;
import com.chixing.pojo.Myorders;
import com.chixing.pojo.TemplateKill;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.xml.ws.Action;
import java.io.IOException;

@Component
public class DeadQueueConsumer {

    @Autowired
    IMyordersService myordersService;

    @Autowired
    TemplateKillMapper templateKillMapper;

    @RabbitHandler
    @RabbitListener(queues = "orderDeadQueue")
    public void fun(String mes,Channel channel, Message message) {
        try {
            Myorders myorders = myordersService.selectByOrderNumber(mes);
            boolean b;
            boolean u;
            if (myorders.getTemplateId() > 9000) {
                //说明是普通模板，删掉该订单即可
                b = myordersService.deleteByOrderNumber(mes);
                u = true;
            }
            else {
                //秒杀模板，删掉订单，回滚库存
                b = myordersService.deleteByOrderNumber(mes);
                //利用乐观锁，库存加一
                TemplateKill templateKill = templateKillMapper.selectById(myorders.getTemplateId());

                if (templateKill == null) {
                    System.out.println("该商品已不存在，选择其它商品");
                    u = false;
                } else {
                    QueryWrapper<TemplateKill> wrapper = new QueryWrapper<>();
                    wrapper.eq("template_kill_id", myorders.getTemplateId()).eq("beiyong1", templateKill.getBeiyong1());
                    templateKill.setBeiyong1(templateKill.getBeiyong1() - 1);
                    //加库存
                    templateKill.setTemplateKillInventory(templateKill.getTemplateKillInventory() + 1);
                    int update = templateKillMapper.update(templateKill, wrapper);
                    if (update > 0) {
                        //加库存成功
                        System.out.println("加库存成功，目前剩余数量：" + templateKill.getTemplateKillInventory());
                        u = true;
                    }else{
                        u = false;
                    }
                }
                if (b && u) {
                    System.out.println("消费者已接收到该消息，并删除了未付费的订单");
                } else {
                    System.out.println("消费者已接收到该消息，但是删除订单失败,可能是该订单已经支付");
                }
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch(Exception e){
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

}
